/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mundo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * @author arquitectura
 */
public class Baraja {
    
    private ArrayList<Carta> baraja;

    public Baraja() {
        baraja=new ArrayList<Carta>();
        String letras=new String("ABCD#");
        String letra=new String(letras.substring(0, 1));
        
        //Carta(String letra, int numero, String nombrePelicula, String nombreDirector, String duraciónPelícula, String galardonesGanados, String anioLanzamiento, String dineroRecaudado, double presupuesto, boolean comodin)

         baraja.add(new Carta("/gui/A1.jpg","/gui/A1_roll.jpg","A",1,"THE SPIRIT","Frank Miller","103","0","2008","50000000",1160447, false));
         baraja.add(new Carta("/gui/A2.jpg","/gui/A2_roll.jpg","A",2,"WATCHMEN","Zack Snyder","162","1","2009","142720410",150000000, false));
         baraja.add(new Carta("/gui/A3.jpg","/gui/A3_roll.jpg","A",3,"THE DARK KNIGHT","Christopher Nolan","152","31","2008","180000000",919000000, false));
         baraja.add(new Carta("/gui/A4.jpg","/gui/A4_roll.jpg","A",4,"The Fast and the Furious (2001)","Rob Cohen","106","8","2001","38000000",144000000, false));
         baraja.add(new Carta("/gui/A5.jpg","/gui/A5_roll.jpg","A",5,"Transformers","Michael Bay","144","12","2007","151000000",708272592, false));
         baraja.add(new Carta("/gui/A6.jpg","/gui/A6_roll.jpg","A",6,"Star Wars: Episode V - The Empire Strikes Back ","Michael Bay","124","11","1980","33000000",573000000, false));
         baraja.add(new Carta("/gui/A7.jpg","/gui/A7_roll.jpg","A",7,"THE MATRIX","Andy Wachowski Larry Wachowski","136","32","1999","64000000",456300000, false));
         baraja.add(new Carta("/gui/A8.jpg","/gui/A8_roll.jpg","A",8,"TERMINATOR","James Cameron","108","1","1984","6500000",78371200, false));
         baraja.add(new Carta("/gui/B1.jpg","/gui/B1_roll.jpg","B",1,"the shining","Stanley Kubrick","142","1","1980","22000000",64984856, false));
         baraja.add(new Carta("/gui/B2.jpg","/gui/B2_roll.jpg","B",2,"THE DA VINCI CODE","Ron Howard","149","0","2006","125000000",758239851, false));
         baraja.add(new Carta("/gui/B3.jpg","/gui/B3_roll.jpg","B",3,"KING KONG","Peter Jackson","187","1","2005","207000000",550517357, false));
         baraja.add(new Carta("/gui/B4.jpg","/gui/B4_roll.jpg","B",4,"THE EXORCIST","William Friedkin","122","14","1973","15000000",402500000, false));
         baraja.add(new Carta("/gui/B5.jpg","/gui/B5_roll.jpg","B",5,"American Pie","Paul Weitz","95","9","1999","11000000",235483004, false));
         baraja.add(new Carta("/gui/B6.jpg","/gui/B6_roll.jpg","B",6,"Forrest Gump","Robert Zemeckis","142","38","1994","55000000",673800000, false));
         baraja.add(new Carta("/gui/B7.jpg","/gui/B7_roll.jpg","B",7,"HairSpray","John Waters","92","0","1988","2000000",8271108, false));
         baraja.add(new Carta("/gui/B8.jpg","/gui/B8_roll.jpg","B",8,"THE SIMPSONS MOVIE","David Silverman","87","2","2007","75000000",527071022, false));
         baraja.add(new Carta("/gui/C1.jpg","/gui/C1_roll.jpg","C",1,"SHREK","Andrew Adamson Vicky Jenson","90","30","2001","60000000",484409218, false));
         baraja.add(new Carta("/gui/C2.jpg","/gui/C2_roll.jpg","C",2,"FINDING NEMO","Andrew Stanton Lee Unkrich","100","35","2003","94000000",86462597, false));
         baraja.add(new Carta("/gui/C3.jpg","/gui/C3_roll.jpg","C",3,"PINOCCIO","Hamilton Luske Ben Sharpsteen","88","4","1940","2600000",84254167, false));
         baraja.add(new Carta("/gui/C4.jpg","/gui/C4_roll.jpg","C",4,"The lion King","Roger Allers Rob Minkoff","89","24","1994","79300000",771900000, false));
         baraja.add(new Carta("/gui/C5.jpg","/gui/C5_roll.jpg","C",5,"P.S. I LOVE YOU","Richard LaGravenese","126","1","2007","30000000",146485050, false));
         baraja.add(new Carta("/gui/C6.jpg","/gui/C6_roll.jpg","C",6,"THE NOTEBOOK","Nick Cassavetes","123","11","2004","30000000",102276787, false));
         baraja.add(new Carta("/gui/C7.jpg","/gui/C7_roll.jpg","C",7,"titanic","James Cameron","194","87","1998","200000000",1835300000, false));
         baraja.add(new Carta("/gui/C8.jpg","/gui/C8_roll.jpg","C",8,"la vita è bella","Roberto Benigni","116","55","1997","3000000",8000000, false));
         baraja.add(new Carta("/gui/D1.jpg","/gui/D1_roll.jpg","D",1,"the godfather","Francis Ford Coppola","175","22","1972","6000000",244900000, false));
         baraja.add(new Carta("/gui/D2.jpg","/gui/D2_roll.jpg","D",2,"Schindler's List","Steven Spielberg","195","70","1993","25000000",317100000, false));
         baraja.add(new Carta("/gui/D3.jpg","/gui/D3_roll.jpg","D",3,"Requiem for a dream","Darren Aronofsky","102","20","2000","4500000",7364000, false));
         baraja.add(new Carta("/gui/D4.jpg","/gui/D4_roll.jpg","D",4,"the professional","Luc Besson","110","2","1994","115000000",1000000000, false));
         baraja.add(new Carta("/gui/D5.jpg","/gui/D5_roll.jpg","D",5,"the lord of the rings: The Fellowship of the Ring","Peter Jackson","178","80","2001","93000000",870761744, false));
         baraja.add(new Carta("/gui/D6.jpg","/gui/D6_roll.jpg","D",6,"harry potter and the Sorcerer's Stone","Chris Columbus","152","16","2001","125000000",976475550, false));
         baraja.add(new Carta("/gui/D7.jpg","/gui/D7_roll.jpg","D",7,"El Laberinto del Fauno","Guillermo del Toro","119","70","2006","19000000",43000000, false));
         baraja.add(new Carta("/gui/D8.jpg","/gui/D8_roll.jpg","D",8,"E. T. The Extra-Terrestrial ","Steven Spielberg","115","42","1982","10500000",792910554, false));
         baraja.add(new Carta("/gui/A1.jpg","/gui/A1_roll.jpg","",-1,"","","","", "", "", 0, true));
    }

    public ArrayList<Carta> getBaraja() {
        return baraja;
    }

    public void setBaraja(ArrayList<Carta> baraja) {
        this.baraja = baraja;
    }
    
    public ArrayList<Carta> repartirCartas()
    {
        List<Carta> aux=new ArrayList(); 
            Random aleatorio = new Random();
            int limite=32;
            int numero=0; 
            for(int i=0;i<33;i++)
            { 
                    try
                    {
                        numero = aleatorio.nextInt(limite);
                        aux.add(this.baraja.get(numero));
                        System.out.println(aux.get(aux.size()-1).getNumero()+aux.get(aux.size()-1).getLetra());
                        baraja.remove(numero);
                    }catch(Exception e)
                    {
                        i--;
                    }
                    
                }
        return (ArrayList<Carta>) aux;
    }
    
    public int jugadaModalidad2(ArrayList<Carta> cartas,String caracteristica, boolean mayor)
    {
        
        boolean esta1A=false;
        boolean estaComodin=false;
        int ubicacion=-1;
        
        for(int i=0;i<cartas.size();i++)
        {
             if(cartas.get(i).isComodin()==true)
            {
                estaComodin=true;
                ubicacion=i;
            }
             else if(cartas.get(i).getLetra().equals("A") && cartas.get(i).getNumero()==1)
             {
                esta1A=true;
             }
        }
        
        if(esta1A==true && estaComodin==true)
        {  
           //Desempatar
            return -2;
        }
        
       
         if(esta1A==false && estaComodin==true)
        {
            return ubicacion;
        }
        
        
         
          if(mayor==true)
          {
              
            if(caracteristica.equalsIgnoreCase("Duracion de Pelicula"))
            {
              int candidata=-1;
              int duracion=Integer.MIN_VALUE;
              int duracion2=0;
              
              for(int i=0;i<cartas.size();i++)
              {
                        duracion2=Integer.parseInt(cartas.get(i).getDuraciónPelícula());
                        
                        if(duracion<duracion2)
                        {
                            duracion=duracion2;
                            candidata=i;
                        }
                        else if(duracion==duracion2)
                        {
                            candidata=-2;
                        }
              }
              return candidata;
            }
            else if(caracteristica.equalsIgnoreCase("Galardones Ganados"))
            {
              int candidata=-1;
              int galardones=Integer.MIN_VALUE;
              int galardones2=0;
              
              for(int i=0;i<cartas.size();i++)
              {
                        galardones2=Integer.parseInt(cartas.get(i).getGalardonesGanados());
                        
                        if(galardones<galardones2)
                        {
                            galardones=galardones2;
                            candidata=i;
                        }
                        else if(galardones==galardones2)
                        {
                            candidata=-2;
                        }
              }
              return candidata;
            }
            else if(caracteristica.equalsIgnoreCase("Año de Lanzamiento"))
            {
              int candidata=-1;
              int anio=Integer.MIN_VALUE;
              int anio2=0;
              
              for(int i=0;i<cartas.size();i++)
              {
                        anio2=Integer.parseInt(cartas.get(i).getAnioLanzamiento());
                        
                        if(anio<anio2)
                        {
                            anio=anio2;
                            candidata=i;
                        }
                        else if(anio==anio2)
                        {
                            candidata=-2;
                        }
              }
              return candidata;
            }
            else if(caracteristica.equalsIgnoreCase("Dinero Recaudado"))
            {
              int candidata=-1;
              double dinero=Double.MIN_VALUE;
              double dinero2=0;
              
              for(int i=0;i<cartas.size();i++)
              {
                        dinero2=Double.parseDouble(cartas.get(i).getDineroRecaudado());
                        
                        if(dinero<dinero2)
                        {
                            dinero=dinero2;
                            candidata=i;
                        }
                        else if(dinero==dinero2)
                        {
                            candidata=-2;
                        }
              }
              return candidata;
            }
            else if(caracteristica.equalsIgnoreCase("Presupuesto"))
            {  
               
              int candidata=-1;
              double presupuesto=Double.MIN_VALUE;
              double presupuesto2=0;
              
              for(int i=0;i<cartas.size();i++)
              {
                        presupuesto2=cartas.get(i).getPresupuesto();
                        
                        if(presupuesto<presupuesto2)
                        {
                            presupuesto=presupuesto2;
                            candidata=i;
                        }
                        else if(presupuesto==presupuesto2)
                        {
                            candidata=-2;
                        }
                        
              }
              return candidata;
            }          
          }
          else
          {
                if(caracteristica.equalsIgnoreCase("Duracion de Pelicula"))
            {
              int candidata=-1;
              int duracion=Integer.MAX_VALUE;
              int duracion2=0;
              
              for(int i=0;i<cartas.size();i++)
              {
                        duracion2=Integer.parseInt(cartas.get(i).getDuraciónPelícula());
                        
                        if(duracion>duracion2)
                        {
                            duracion=duracion2;
                            candidata=i;
                        }
                        else if(duracion==duracion2)
                        {
                            candidata=-2;
                        }
              }
              return candidata;
            }
            else if(caracteristica.equalsIgnoreCase("Galardones Ganados"))
            {
              int candidata=-1;
              int galardones=Integer.MAX_VALUE;
              int galardones2=0;
              
              for(int i=0;i<cartas.size();i++)
              {
                        galardones2=Integer.parseInt(cartas.get(i).getGalardonesGanados());
                        
                        if(galardones>galardones2)
                        {
                            galardones=galardones2;
                            candidata=i;
                        }
                        else if(galardones==galardones2)
                        {
                            candidata=-2;
                        }
              }
              return candidata;
            }
            else if(caracteristica.equalsIgnoreCase("Año de Lanzamiento"))
            {
              int candidata=-1;
              int anio=Integer.MAX_VALUE;
              int anio2=0;
              
              for(int i=0;i<cartas.size();i++)
              {
                        anio2=Integer.parseInt(cartas.get(i).getAnioLanzamiento());
                        
                        if(anio>anio2)
                        {
                            anio=anio2;
                            candidata=i;
                        }
                        else if(anio==anio2)
                        {
                            candidata=-2;
                        }
              }
              return candidata;
            }
            else if(caracteristica.equalsIgnoreCase("Dinero Recaudado"))
            {
              int candidata=-1;
              double dinero=Double.MAX_VALUE;
              double dinero2=0;
              
              for(int i=0;i<cartas.size();i++)
              {
                        dinero2=Double.parseDouble(cartas.get(i).getDineroRecaudado());
                        
                        if(dinero>dinero2)
                        {
                            dinero=dinero2;
                            candidata=i;
                        }
                        else if(dinero==dinero2)
                        {
                            candidata=-2;
                        }
              }
              return candidata;
            }
            else if(caracteristica.equalsIgnoreCase("Presupuesto"))
            {  
                System.out.println("a la menor");
              int candidata=-1;
              double presupuesto=Double.MAX_VALUE;
              double presupuesto2=0;
              
              for(int i=0;i<cartas.size();i++)
              {
                        presupuesto2=cartas.get(i).getPresupuesto();
                        
                        if(presupuesto>presupuesto2)
                        {
                            presupuesto=presupuesto2;
                            candidata=i;
                        }
                        else if(presupuesto==presupuesto2)
                        {
                            candidata=-2;
                        }
              }
              return candidata;
            }  
          }
        
        return 0;
    }
    
     public Carta jugadaModalidad1(int numero, ArrayList<Carta>oponente)
    {
        Carta aux=null;
        for(int i=0; i<oponente.size();i++)
        {
            if(oponente.get(i).getNumero()==numero)
            {
                System.out.println("HOLAA");
                aux=oponente.get(i);
               oponente.remove(i);
                return aux;
            }
        }
        return aux;
    }

    public boolean validarCuarteto(ArrayList<Carta>cartas,int numero)
    {
        int contador=0;
        for(int i=0;i<cartas.size();i++)
        {
            if(numero==cartas.get(i).getNumero())
            {
                contador++;
            }
        }

        if(contador==4)
            return true;

        return false;

    }

    public int contarCuartetos(ArrayList<Carta> jugador)
    {
        int contador=0;
        ArrayList<Carta> jugadoraux=new ArrayList();
        jugadoraux=jugador;
        for(int i=0;i<jugador.size();i++)
        {
            for(int j=0;j<jugadoraux.size();j++)
            {
                if(jugador.get(i).getNumero()==jugadoraux.get(j).getNumero())
                {
                    contador++;
                }

            }
        }

        if(contador>15)
        {
            return(contador/16);
        }
        else
        {
            return 0;
        }

    }

    public boolean esta1A(ArrayList<Carta>cartas)
    {
        for(int i=0;i<cartas.size();i++)
        {
            if((cartas.get(i).getNumero()==1)&&(cartas.get(i).getLetra().equalsIgnoreCase("A")))
            {
                return true;
            }
        }
        return false;
    }
}
